package com.taiyuan.tongcheng.ui.fragment;

import org.kymjs.kjframe.ui.AnnotateUtil;
import org.kymjs.kjframe.ui.BindView;
import org.kymjs.kjframe.widget.RoundImageView;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.taiyuan.tongcheng.adapter.OderStationAdapter;
import com.taiyuan.tongcheng.application.TCBroadcastAction;
import com.taiyuan.tongcheng.bean.UserInfo;
import com.taiyuan.tongcheng.db.TcDbHelper;
import com.taiyuan.tongcheng.http.TcWebservice;
import com.taiyuan.tongcheng.ui.AboutActivty;
import com.taiyuan.tongcheng.ui.CardListActivity;
import com.taiyuan.tongcheng.ui.LoginActivity;
import com.taiyuan.tongcheng.ui.OrderActivity;
import com.taiyuan.tongcheng.ui.PersonInfoActivity;
import com.taiyuan.tongcheng.ui.SettingActivity;
import com.taiyuan.tongcheng.ui.ShareActvity;
import com.taiyuan.tongcheng.ui.TcWebviewActivity;
import com.taiyuan.tongcheng.util.SharePreferenceUtil;
import com.tytc56.user.R;

public class MenuLeftFragment extends BaseFragment implements OnClickListener{
	
	//widget 个人信息
	@BindView(id=R.id.img_usercenter_head,click=true)
	private RoundImageView  roundImgView;
	@BindView(id=R.id.tv_usercenter_name)
	private TextView  userName;
	@BindView(id=R.id.tv_usercenter_tel)
	private TextView  userTel;
	@BindView(id=R.id.tv_usercenter_info,click=true)
	private TextView  userStateInfoTv;
	// 功能项
	@BindView(id=R.id.rl_user_coupon,click=true)
	private RelativeLayout couponLayout;
	@BindView(id=R.id.rl_user_share,click=true)
	private RelativeLayout shareLayout;
	@BindView(id=R.id.rl_user_inform,click=true)
	private RelativeLayout  informLayout;
	@BindView(id=R.id.rl_user_about,click=true)
	private RelativeLayout  aboutLayout;
	@BindView(id=R.id.rl_user_setting,click=true)
	private RelativeLayout  settingLayout;
	@BindView(id=R.id.rl_user_order,click=true)
	private RelativeLayout  orderLayout;
	@BindView(id=R.id.btn_exit, click=true)
	private Button exitBtn;
	//数据
	private View contentView;
	private UserInfo  user=new UserInfo();
    
	private  IntentFilter intentFilter;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		contentView =inflater.inflate(R.layout.fragment_tab3, null);
		// 注解
		AnnotateUtil.initBindView(this, contentView);
		initView();
		registReceiver();
		return contentView;
	}
     
	
	
	
	private void registReceiver() {
		intentFilter=new IntentFilter();
		intentFilter.addAction(TCBroadcastAction.SUCCESS_LOGIN);
		getActivity().registerReceiver(receiver, intentFilter);
	}
	
	private BroadcastReceiver  receiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if(TCBroadcastAction.SUCCESS_LOGIN.equals(intent.getAction())){
				refreshUi();
			}
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		refreshUi();
	}
	
	@Override
	public void onClick(View v) {
		
		switch (v.getId()) {
		
		case R.id.rl_user_order:
			startActivity(OrderActivity.class);
			break;
		case R.id.tv_usercenter_info:
			if(spUtil.getLoginFlag()==0){
				Intent loginIntent=new Intent(getActivity(),LoginActivity.class);
				loginIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				startActivity(loginIntent);
			}else{
				startActivity(PersonInfoActivity.class);
			}
			break;
			
		case R.id.rl_user_about:
			startActivity(AboutActivty.class);
			break;
		case R.id.rl_user_coupon:
			startActivity(CardListActivity.class);
			break;
		case R.id.rl_user_info:
			
			break;
		case R.id.rl_user_inform:
			Bundle  bundle =new Bundle();
			bundle.putInt(TcWebviewActivity.TAG_COMFROM, TcWebviewActivity.TAG_HUZHU_INFROM);
			bundle.putString(TcWebviewActivity.TAG_TOP_TITLE, getString(R.string.user_notice));
			startActivity(TcWebviewActivity.class, bundle);
			break;
		case R.id.rl_user_setting:
			startActivity(SettingActivity.class);
			break;
		case R.id.rl_user_share:
			startActivity(ShareActvity.class);;
			break;
		case R.id.btn_exit:
			spUtil.setLoginFlag(0);
			TcDbHelper.deleteUser(getActivity());
			refreshUi();
			break;
		default:
			break;
		}
		
	}
	
	@Override
	protected void initView() {
		super.initView();
		refreshUi();
		couponLayout.setOnClickListener(this);
		shareLayout.setOnClickListener(this);
		orderLayout.setOnClickListener(this);
		informLayout.setOnClickListener(this);
		settingLayout.setOnClickListener(this);
		aboutLayout.setOnClickListener(this);
	}
	
	private void refreshUi() {
		user = TcDbHelper.getUser(getActivity());
		if (user != null) {
			userTel.setVisibility(View.VISIBLE);
			userName.setVisibility(View.VISIBLE);
			userName.setText(user.getName());
			userTel.setText(user.getMobile() + "");
			userStateInfoTv.setText(R.string.user_status_login);
			exitBtn.setVisibility(View.VISIBLE);
		} else {
			userTel.setVisibility(View.GONE);
			userName.setVisibility(View.GONE);
			userStateInfoTv.setText(R.string.user_status_nologin);
			exitBtn.setVisibility(View.GONE);
		}
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		getActivity().unregisterReceiver(receiver);
	}
}
